<?php

namespace plugin\admin\app\controller\install;

class TOOLS
{
    static public function checkEnv()
    {
        $items = [
            [   //os
                'title'   => '操作系统',
                'limit'   => '不限制',
                'current' => PHP_OS,
                'icon'    => "el-icon-circle-check",
            ],
            [   //php
                'title'   => 'PHP版本',
                'limit'   => '7.3+',
                'current' => PHP_VERSION,
                'icon'    => 'el-icon-circle-check',
            ],
            [   //upload
                'title'   => '附件上传',
                'limit'   => '不限制',
                'current' => ini_get('file_uploads') ? ini_get('upload_max_filesize') : '未知',
                'icon'    => 'el-icon-circle-check',
            ],
            [   //gd
                'title'   => 'GD库',
                'limit'   => '2.0+',
                'current' => '未知',
                'icon'    => 'el-icon-circle-check',
            ],
            [   //disk
                'title'   => '磁盘空间',
                'limit'   => '200M+',
                'current' => '未知',
                'icon'    => 'el-icon-circle-check',
            ]
        ];
        //PHP环境检测
        if ($items[1]['current'] < 7.3) {
            $items[1]['icon'] = 'fa-remove text-danger';
            session(['error'=>true]);
        }

        //GD库检测
        $tmp = function_exists('gd_info') ? gd_info() : [];
        if (!$tmp['GD Version']) {
            $items[3]['current'] = '未安装';
            $items[3]['icon']    = 'el-icon-circle-close';
            session(['error'=>true]);
        } else {
            $items[3]['current'] = $tmp['GD Version'];
        }
        unset($tmp);
        //磁盘空间检测
        if (function_exists('disk_free_space')) {
            $disk_size                = floor(disk_free_space('./') / (1024 * 1024));
            $items[4]['current'] = $disk_size . 'MB';
            if ($disk_size < 200) {
                $items[4]['icon'] = 'el-icon-circle-close';
                session(['error'=>true]);
            }
        }
        return $items;
    }

    static public function checkDirfile(){
        $database_config_file = base_path() . '/plugin/admin/config';
        $upload_file = base_path() . '/plugin/admin/public/upload';
        $items = array(
            '0' => array(
                'type'  => 'dir',
                'path'  => runtime_path(),
                'title' => '可写',
                'current' => '可写',
                'icon'  => 'el-icon-circle-check',
            ),
            '1' => array(
                'type'  => 'dir',
                'path'  => $upload_file,
                'title' => '可写',
                'current' => '可写',
                'icon'  => 'el-icon-circle-check',
            ),
            '2' => array(
                'type'  => 'dir',
                'path'  => $database_config_file,
                'title' => '可写',
                'current' => '可写',
                'icon'  => 'el-icon-circle-check',
            ),
        );

        foreach ($items as &$val) {
            $path = $val['path'];
            if ('dir' === $val['type']) {
                if (!is_writable($path)) {
                    if (is_dir($path)) {
                        $val['current'] = '不可写';
                        $val['icon']  = 'el-icon-circle-close';
                        session(['error'=>true]);
                    } else {
                        $val['current'] = '不存在';
                        $val['icon']  = 'el-icon-circle-close';
                        session(['error'=>true]);
                    }
                }
            } else {
                if (file_exists($path)) {
                    if (!is_writable($path)) {
                        $val['current'] = '不可写';
                        $val['icon']  = 'el-icon-circle-close';
                        session(['error'=>true]);
                    }
                } else {
                    if (!is_writable(dirname($path))) {
                        $val['current'] = '不存在';
                        $val['icon']  = 'el-icon-circle-close';
                        session(['error'=>true]);
                    }
                }
            }
        }
        return $items;
    }

    static public function checkFuncAndExt(){
        $items = array(
            '0' => array(
                'type'    => 'ext',
                'name'    => 'pdo',
                'title'   => '支持',
                'current' => extension_loaded('pdo'),
                'icon'    => 'el-icon-circle-check',
            ),
            '1' => array(
                'type'    => 'ext',
                'name'    => 'pdo_mysql',
                'title'   => '支持',
                'current' => extension_loaded('pdo_mysql'),
                'icon'    => 'el-icon-circle-check',
            ),
            '2' => array(
                'type'  => 'func',
                'name'  => 'file_get_contents',
                'title' => '支持',
                'icon'  => 'el-icon-circle-check',
            ),
            '3' => array(
                'type'  => 'func',
                'name'  => 'mb_strlen',
                'title' => '支持',
                'icon'  => 'el-icon-circle-check',
            ),
        );
        foreach ($items as &$val) {
            switch ($val['type']) {
                case 'ext':
                    if (!$val['current']) {
                        $val['title'] = '不支持';
                        $val['icon']  = 'el-icon-circle-close';
                        session(['error'=>true]);
                    }
                    break;
                case 'func':
                    if (!function_exists($val['name'])) {
                        $val['title'] = '不支持';
                        $val['icon']  = 'el-icon-circle-close';
                        session(['error'=>true]);
                    }
                    break;
            }
        }
        return $items;
    }

    static public function checkInstall(){
        $database_config_file = base_path() . '/plugin/admin/config/database.php';
        clearstatcache();
        if (is_file($database_config_file)) {
            abort(1001,'系统已安装',config('plugin.admin.app.version'));
        }
    }
}